package cn.nb12.xbox.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.nb12.xbox.Param;
import cn.nb12.xbox.po.Area;
import cn.nb12.xbox.po.Games;
import cn.nb12.xbox.service.AreaService;
import cn.nb12.xbox.service.GamesService;

@Component
public class UpdateAllGame {
	
	private Logger log=LoggerFactory.getLogger(UpdateAllGame.class);
	
	@Autowired
	private GamesService gamesService;
	
	@Autowired
	private GenerateBusinessCodeService generateBusinessCodeService;
	
	@Autowired
	private AreaService areaService;
	
	
	/**
	 * 更新数据库所有游戏
	 */
	public void updateAllGame() {
		try {
			//获取所有游戏代码
			List<String> data = GetGame.getData();
			if(data!=null && data.size()>0) {
				for (String code : data) {
					log.info("开始更新游戏信息");
					//根据游戏代码查询游戏
					Assist assist=new Assist();
					assist.setRequires(Assist.andEq("code", code));
					List<Games> list = gamesService.selectGames(assist);
					if(list!=null && list.size()>0) {//查到数据
						//不做处理
					}
					else {//没有数据
						Games games=new Games();
						games.setCode(code);
						games.setId(generateBusinessCodeService.doWork());
						gamesService.insertGames(games);//更新到数据库
					}
				}
				//获取所有的地区
				List<Area> areas = areaService.selectArea(null);
				if(areas!=null && areas.size()>0) {//地区表不为空
					log.info("更新费率");
					//更新地区费率
					for (Area area : areas) {
						String exchangeRate = ExchangeRateUtil.getExchangeRate(area.getCurrency(), Param.CHINA_CURRENCY);
						if(exchangeRate!=null && exchangeRate.trim().length()>0) {
							area.setRate(Double.parseDouble(exchangeRate.trim()));//汇率
							area.setUpdatetime(DateUtil.getNowDate1());//更新时间
							areaService.updateNonEmptyAreaById(area);
						}
					}
					//重组所有价格信息
					List<Games> games1 = gamesService.selectGames(null);//查询所有游戏
					Vector<Games> games=new Vector<>();
					games.addAll(games1);
					if(games!=null && games.size()>0) {//有游戏数据
						log.info("更新所有价格");
						List<GameUpdate> threads=new ArrayList<>();
						for (Area area : areas) {//创建线程
							threads.add(new GameUpdate( games, area,generateBusinessCodeService));
						}
						for (GameUpdate gameUpdate : threads) {//启动线程
							gameUpdate.start();
						}
						for (GameUpdate gameUpdate : threads) {//主线程等待处理完毕
							gameUpdate.join();
						}
						for (Games game : games) {
							gamesService.updateNonEmptyGamesById(game);
						}
					}
				}
			}
			log.info("更新成功");
		} catch (Exception e) {
			log.error("更新数据错误",e);
		}
		log.info("更新完毕");
	}
	
	/**
	 * 更新初始化的数据
	 */
	public void updateInitData() {
		try {
			Assist assist=new Assist();
			assist.setRequires(new Assist().new WhereRequire<String>(" and imgurl is null ", null));
			List<Games> games = gamesService.selectGames(assist);
			//获取所有的地区
			List<Area> areas = areaService.selectArea(null);
			if(areas!=null && games!=null && areas.size()>0 && games.size()>0) {//有数据
				if(games!=null && games.size()>0) {//有游戏数据
					log.info("更新未更新的游戏");
					Vector<GameUpdate> threads=new Vector<>();
					for (Area area : areas) {//创建线程
						threads.add(new GameUpdate(games, area,generateBusinessCodeService));
					}
					for (GameUpdate gameUpdate : threads) {//启动线程
						gameUpdate.start();
					}
					for (GameUpdate gameUpdate : threads) {//主线程等待处理完毕
						gameUpdate.join();
					}
					for (Games game : games) {
						gamesService.updateNonEmptyGamesById(game);
					}
				}
			}
		} catch (Exception e) {
			log.error("更新部分游戏失败",e);
		}
	
	}
	
	/**
	 * 更新指定游戏
	 */
	public void updateData(Vector<Games> games) {
		try {
			//获取所有的地区
			List<Area> areas = areaService.selectArea(null);
			if(areas!=null && games!=null && areas.size()>0 && games.size()>0) {//有数据
				if(games!=null && games.size()>0) {//有游戏数据
					log.info("更新未更新的游戏");
					Vector<GameUpdate> threads=new Vector<>();
					for (Area area : areas) {//创建线程
						threads.add(new GameUpdate( games, area,generateBusinessCodeService));
					}
					for (GameUpdate gameUpdate : threads) {//启动线程
						gameUpdate.start();
					}
					for (GameUpdate gameUpdate : threads) {//主线程等待处理完毕
						gameUpdate.join();
					}
					for (Games game : games) {
						gamesService.updateNonEmptyGamesById(game);
					}
				}
			}
		} catch (Exception e) {
			log.error("更新部分游戏失败",e);
		}
	
	}
	
}

